#/bin/bash
export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

#if [[ "$(whoami)" != "root" ]]; then
#       echo "please run this script as root" >&2
#       exit 1
#fi

User='root'
Pass='1zhuhai-qwer'
Ip='192.168.30.22'

Backup_dir="/home/mysql_backup_file"

find ${Backup_dir} -type d -mtime +10  | xargs -i ls -l {}
find ${Backup_dir} -type d -mtime +10  | xargs -i rm -rf {}

find ${Backup_dir} -type d -mtime +10  | xargs -i ls -l {}
find ${Backup_dir} -type d -mtime +10  | xargs -i rm -rf {}

mysql_login="mysql -u${User} -p${Pass} -h${Ip}" 
Mysqldump="mysqldump -u${User} -p${Pass} -h${Ip} -F --single-transaction --events --flush-privileges --triggers --routines --hex-blob -B"

mkdir -p ${Backup_dir}/$(date +%F)_iZH_Analysis_iZH_Staging
#data=${Backup_dir}/backup_data_$(date +%Y%m%d).sql.gz
Log_file=${Backup_dir}/$(date +%F)_iZH_Analysis_iZH_Staging/backup_$(date +%F).log

for database in `${mysql_login} -e "show databases;" | sed -e '1d' | grep -P "iZH_Analysis|iZH_Staging"`
do
        echo $(date "+%F %H:%M:%S") ${database} >> ${Log_file}
        $Mysqldump $database | gzip > ${Backup_dir}/$(date +%F)_iZH_Analysis_iZH_Staging/${database}_$(date +%F).sql.gz
done

#for database in `${mysql_login} -e "show databases;" | sed -e '1d' | grep -P "iZH_Light_Manager"`
for database in `${mysql_login} -e "show databases;" | sed -e '1d' | grep -P "iZH_Manager"`
do
        echo $(date "+%F %H:%M:%S") ${database} >> ${Log_file}
        $Mysqldump $database | gzip > ${Backup_dir}/$(date +%F)_iZH_Analysis_iZH_Staging/${database}_$(date +%F).sql.gz
done
